import { showLoading, hideLoading } from 'react-redux-loading-bar';
import { queryNodeLogList } from '../services/nodeLogService';
import { PAGE_SIZE } from '../constants/constants.js';

export default {
  namespace: 'nodeLog',
  state: {
    pageNum: 1,
    pageSize: PAGE_SIZE,
    total: 0,
    identity: '',
    event: '',
    startTime: null,
    endTime: null,
    list: [],
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
      return history.listen(({ pathname }) => {
        if (pathname === '/log/nodeLog') {
          dispatch({
            type: 'queryNodeLogList',
            payload: {
              pageNum: 1,
              pageSize: PAGE_SIZE,
            },
          });
        }
      });
    },
  },

  effects: {
    *queryNodeLogList({ payload, }, { call, put, select }) { // eslint-disable-line
      try {
        yield put(showLoading());// 开始顶部进度条
        const { data } = yield call(queryNodeLogList, payload);
        if (data.code === 200) {
          yield put({
            type: 'save',
            payload: {
              list: data.data.content,
              total: data.data.totalElements,
              pageNum: data.data.number,
              pageSize: payload.pageSize,
              event: payload.event,
              identity: payload.identity,
              startTime: payload.startLogTime,
              endTime: payload.endLogTime,
            },
          });
        }
      } finally {
        yield put(hideLoading());// 结束顶部进度条
      }
    },
  },
  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },
  },

};
